import GameModel from '../GameModel';
import Scene from '../Scene';
import WxUtils from '../WxUtils';

const { ccclass, property } = cc._decorator;

@ccclass
export default class SceneOpen extends Scene {

    @property(cc.Node)
    btnLogin: cc.Node = null;

    private _model: GameModel;
    private btnAuthorize: wx.UserInfoButton;

    protected onLoad(): void {
        super.onLoad();
        this._model = GameModel.getInst();
        this.btnAuthorize = WxUtils.createAuthorizeBtn(this.btnLogin, this.onAuthorize.bind(this));
    }

    protected onEnable(): void {
        super.onEnable();
        this.btnAuthorize.show();
    }

    protected onDisable(): void {
        this.btnAuthorize.hide();
        super.onDisable();
    }

    protected onDestroy(): void {
        this.btnAuthorize.destroy();
    }

    private gotoHall(): void {
        cc.director.loadScene("Hall");
    }

    private onAuthorize(res: any): void {
        console.log("authorize: ", res);
        this._model.userInfo = res;
        if (res.userInfo) {
            this.btnAuthorize.hide();
            this.scheduleOnce(() => {
                this.gotoHall();
            }, 2);
        }
        else {
            wx.showToast({
                title: '获取用户信息失败,请到“菜单>关于>设置”中，开启权限',
                icon: 'none',
                duration: 2000
            });
        }
    }
}
